// pages/home/home.js
const app = getApp()
wx.cloud.init({
    env: 'cloud1-8gbitjqz4bc93039',
})
const db = wx.cloud.database()
var id = 0
Page({

    /**
     * 页面的初始数据
     */
    data: {
        c: [],
        idx: 0
    },
    readed(notice) {
        console.log(notice)
        var s = notice.notReaded.split(' ')
        console.log(s)
        var ss = []
        for (var i = 0; i < s.length; i++) {
            if (s[i] == app.globalData.sList[0].name) {
                continue
            } else {
                ss.push(s[i])
            }
        }
        var nR = ""
        for (var i = 0; i < ss.length - 1; i++) {
            nR += ss[i] + ' '
        }
        console.log(nR)
        db.collection("notice").where({
            _id: notice._id
        }).update({
            data: {
                notReaded: nR
            }
        })
    },

    showStat(){
        var NL = this.data.stats
        console.log(NL)
        if (NL.length == 0)
        {
            return
        }
            
        var stat = NL[0]
        NL.shift()
        this.setData({
            stat: NL
        })
        if (stat.doneList.split(' ').findIndex((item, index, arr) => {
                return item == app.globalData.sList[0].name
            }) != -1) {
            this.showStat()
            return
        }

        let that = this
        wx.showModal({
            title: stat.title,
            showCancel: true,
            confirmText: stat.choiceA,
            cancelText: stat.choiceB,
            complete: (res) => {
                console.log(res)
                that.statComplete(stat,res)
                that.showStat()
            }
            
        })
    },

    statComplete(stat,res)
    {
        if(res.confirm)
        {
            stat.AList+=app.globalData.sList[0].name+' '
        }
        else
        {
            stat.BList+=app.globalData.sList[0].name+' '
        }
        stat.doneList+=app.globalData.sList[0].name+' '
        stat.done++
        db.collection('statistics').where({
            _id: stat._id
        }).update({
            data: {
                AList:stat.AList,
                BList:stat.Blist,
                doneList:stat.doneList,
                done:stat.done
            }
        })
    },

    showNotice() {
        var NL = this.data.notices
        if (NL.length == 0)
        {
            this.showStat()
            return
        }
            
        var notice = NL[0]
        NL.shift()
        this.setData({
            notices: NL
        })
        if (notice.notReaded.split(' ').findIndex((item, index, arr) => {
                return item == app.globalData.sList[0].name
            }) == -1) {
            this.showNotice()
            return
        }

        let that = this
        wx.showModal({
            title: notice.topic,
            content: notice.text,
            showCancel: false,
            confirmText: "确认收到",
            complete: (res) => {
                console.log(res)
                that.showNotice()
                that.readed(notice)
            }
        })
    },

    async init()
    {
        if (!this.data.isT) {
            var MAX_LIMIT = 20
            var countResult = await db.collection('notice')
                .where({
                    class: app.globalData.sList[0].class
                }).count()
            console.log(app.globalData.tInfo.name)
            console.log(countResult)
            var total = countResult.total
            var times = Math.ceil(total / MAX_LIMIT)
            var tmpList = []
            for (let i = 0; i < times; i++) {
                await db.collection('notice')
                    .skip(i * MAX_LIMIT)
                    .limit(MAX_LIMIT)
                    .where({
                        class: app.globalData.sList[0].class
                    })
                    .get().then(res => {
                        for (let j = 0; j < res.data.length; j++) {
                            tmpList.push(res.data[j])
                        }
                    })
            }
            console.log(tmpList)
            this.setData({
                notices:tmpList
            })
            countResult = await db.collection('statistics')
                .where({
                    classId: app.globalData.sList[0].class
                }).count()
            console.log(app.globalData.tInfo.name)
            console.log(countResult)
            total = countResult.total
            times = Math.ceil(total / MAX_LIMIT)
            var tmpList = []
            for (let i = 0; i < times; i++) {
                await db.collection('statistics')
                    .skip(i * MAX_LIMIT)
                    .limit(MAX_LIMIT)
                    .where({
                        classId: app.globalData.sList[0].class
                    })
                    .get().then(res => {
                        for (let j = 0; j < res.data.length; j++) {
                            tmpList.push(res.data[j])
                        }
                    })
            }
            console.log(tmpList)
            this.setData({
                stats:tmpList
            })
            this.showNotice()
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            isT: app.globalData.isTeacher
        })
        this.init()
    },
    nexta: function () {
        var cc = this.data.c
        var idxx = this.data.idx
        cc[idxx] = "ripple fadeInRight"
        idxx++
        console.log(idxx)
        if (idxx > 8) {
            clearInterval(id);
        }
        this.setData({
            c: cc,
            idx: idxx
        })
    },
    aniStart: function () {
        this.setData({
            idx: 0
        })
        var that = this
        id = setInterval(function () {
            that.nexta()
        }, 80)
        console.log(id)
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.aniStart()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})